#include "a9main.h"

//创建一个空的队列
linkqueue_t *linkqueue_create()
{
	linkqueue_t *lq = (linkqueue_t *)malloc(sizeof(linkqueue_t));
	lq->front = lq->rear = (linknode_t *)malloc(sizeof(linknode_t));

	lq->front->next = NULL;

	return lq;
}

//判断队列是否为空
int linkqueue_empty(linkqueue_t *lq)
{
	return lq->front == lq->rear ? 1 : 0;
}

//入队
int linkqueue_push(linkqueue_t *lq, datatype_t value)
{
	linknode_t *temp = (linknode_t *)malloc(sizeof(linknode_t));
	temp->data = value;
	temp->next = NULL;

	lq->rear->next = temp;  //将插入的结点temp放在rear所指向的结点的后面
	lq->rear = temp;  //让rear指向组后一个结点

	return 0;
}

//出队,保留头结点
M0_cmd linkqueue_pop(linkqueue_t *lq)
{

	linknode_t *temp = lq->front->next;
	datatype_t value;

	lq->front->next = temp->next;

	value = temp->data;

	free(temp);
	temp = NULL;

	if(lq->front->next == NULL)
	{
		lq->rear = lq->front;
	}

	return value;
}


